import { Component } from '@angular/core';

import { NavParams, ViewController } from 'ionic-angular';

import { TaskDetailService } from './detail.service';

import { Config } from '../../../providers/config';

import _ from 'lodash';


@Component({
  selector: 'page-task-cashPlan',
  templateUrl: './cashPlanDetail.html',
  providers: [TaskDetailService]
})
export class CashPlanPage {
	moa_page_name: any = "page-task-cashPlan";
  moa_page_explain: any = "流程 - 付款计划列表";

  _ = _;
  // 明细列表数据
  cashPlanList = [];

  // 流程文件ID
  bid;

  // 需要显示的字段
  feilds = ["periods","money","type","preTime","transfer"];


  constructor(
    public navParams: NavParams,
    public viewCtrl: ViewController,
    public taskDetailService: TaskDetailService
  ) {
    this.bid = this.navParams.get('bid');
    this.loadDate(this.bid);
  }

  loadDate(bid){
    console.log(bid);
    this.taskDetailService.getCashPlanDetail(bid).subscribe(data => {
      if(data) {
        this.formatedData(data);
        console.log(this.cashPlanList);
      }
    });
  }

  complete() {
    this.dismiss();
  }

  dismiss(data?: any) {
    // using the injected ViewController this page
    // can "dismiss" itself and pass back data
    this.viewCtrl.dismiss(data);
  }

  checkIsSelected(item) {
    item.isShow = !item.isShow;
  }

 formatedData(cashPlanList){
   var that = this;
   _.forEach(cashPlanList,function(data){
     let tempList = [];
     let item ={}

     // 期数
     item["key"] = "期数"
     item["vaule"] = _.find(data,{"fieldid": "periods"})["fvalue"];

     tempList.push(item);

     // 金额
     item ={}
     item["key"] = "金额"
     item["vaule"] = _.find(data,{"fieldid": "money"})["fvalue"];
     tempList.push(item);

     // 性质
     item ={}
     item["key"] = "性质"
     item["vaule"] = _.find(data,{"fieldid": "type"})["fvalue"] == "fukuan" ? "付款" : "收款";
     tempList.push(item);

     // 预计时间
     item ={}
     item["key"] = "预计时间"
     item["vaule"] = _.find(data,{"fieldid": "preTime"})["fvalue"];
     tempList.push(item);

     // 结算方式
     item ={}
     item["key"] = "结算方式"
     let preTimeNode = _.find(data,{"fieldid": "cashForm"});
     console.log(preTimeNode);
     let preTimeFvalue = preTimeNode["fvalue"];
     item["vaule"] = _.find(preTimeNode["fieldvalDtos"] ,{"fvau":preTimeFvalue})["fdval"];
     tempList.push(item);

     that.cashPlanList.push(tempList)

   });
 }
}
